package com.supermap.ar.unity;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Keep;

/**
 * 安卓工具类
 */
public class AndroidToolkit {
    /**
     * unity项目启动时的的上下文
     */
    private static Activity mUnityActivity;
    /**
     * 获取unity项目的上下文
     * @return
     */
    public static Activity getActivity(){
        if(null == mUnityActivity) {
            try {
                Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
                Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
                mUnityActivity = activity;
            } catch (ClassNotFoundException e) {

            } catch (IllegalAccessException e) {

            } catch (NoSuchFieldException e) {

            }
        }
        return mUnityActivity;
    }

    /**=================Unity 中调用的方法- start =================*/
    /**
     * Toast显示unity发送过来的内容
     * @param msg 消息的内容
     * @return 调用是否成功
     */
    @Keep
    static void ShowToast(String msg){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Keep
    static void LogI(String tag,String msg){
        Log.i(tag, msg);
    }
    @Keep
    static void LogE(String tag,String msg){
        Log.e(tag, msg);
    }
    @Keep
    static void LogV(String tag,String msg){
        Log.v(tag, msg);
    }
    @Keep
    static void LogD(String tag,String msg){
        Log.d(tag, msg);
    }
    @Keep
    static void LogW(String tag,String msg){
        Log.w(tag, msg);
    }
    /**=================Unity 中调用的方法-  end  =================*/

}
